import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import type { ExamQuestionAnswerVo, ExamQuestionCollectVo, QuestionAnswerError, KnowExerciseProgress } from '@/api/exam/types'
import type { AnswerTopicIndex, KnowExerciseIndex } from '@/api/constants'
export const useExericseStore = defineStore('exercise_store',
  () => {
    /**
     * 答题记录
     */
    const questionAnswers = ref<ExamQuestionAnswerVo[]>([])

    /**
     *错题记录
     */
    const questionAnswerErrors = ref<QuestionAnswerError[]>([])

    /**
     * 试题收藏
     */
    const questionCollects = ref<ExamQuestionCollectVo[]>([])

    /**
     * 答题位置数组对象
     */
    const answerTopicIndexs = ref<AnswerTopicIndex[]>([])

    /**
     * 当前练习的考点
     */
    const knowExerciseIndexs = ref<KnowExerciseIndex[]>([])

    /**
     * 考点练习进度
     */
    const knowsExerciseProgress = ref<KnowExerciseProgress[]>([])

    /**
     * 添加答题记录
     * @param questionAnswer
     */
    const addQuestionAnswer = (questionAnswer: ExamQuestionAnswerVo) => {
      // 判断是否答题是否存在
      let existIndex = questionAnswers.value.findIndex((item) => {
        return item.questionId === questionAnswer.questionId
      })

      // 存在删除
      if (existIndex >= 0) {
        questionAnswers.value.splice(existIndex, 1)
      }
      // 添加答题记录
      questionAnswers.value.unshift(questionAnswer)

      // 错题
      if (questionAnswer.status === 0) {
        let errorCount = 1
        // 错题是否存在
        let wrongExistIndex = questionAnswerErrors.value.findIndex((item) => {
          return item.questionId === questionAnswer.questionId
        })

        // 存在删除
        if (wrongExistIndex >= 0) {
          errorCount = questionAnswerErrors.value[wrongExistIndex].errorCount + 1
          questionAnswerErrors.value.splice(wrongExistIndex, 1)
        }
        // 添加错题
        questionAnswerErrors.value.unshift({ ...questionAnswer, ... { errorCount } })
      }
    }

    /**
     *
     * @param val 添加收藏试题
     */
    const addQuestionCollect = (val: ExamQuestionCollectVo) => {
      // 判断是否答题是否存在
      let existIndex = questionCollects.value.findIndex((item) => {
        return item.questionId === val.questionId
      })

      // 存在删除
      if (existIndex >= 0) {
        questionCollects.value.splice(existIndex, 1)
      }
      // 添加收藏试题
      questionCollects.value.unshift(val)
    }

    /**
    * 移除收藏试题
    * @param val
    */
    const removeQuestionCollect = (val: number) => {
      // 判断是否答题是否存在
      let existIndex = questionCollects.value.findIndex((item) => {
        return item.questionId === val
      })

      // 存在删除
      if (existIndex >= 0) {
        questionCollects.value.splice(existIndex, 1)
      }
    }

    /**
    *设置答题位置
    * @param id
    * @param topicIndex
    */
    const setAnswerTopicIndex = (id: string, topicIndex: number) => {
      let answerTopicIndex = answerTopicIndexs.value.find(item => item.id === id)
      if (answerTopicIndex) {
        answerTopicIndex.topicIndex = topicIndex
      } else {
        answerTopicIndexs.value.push({ id, topicIndex })
      }
    }

    /**
      *获取答题位置
      * @param id
      * @param topicIndex
      */
    const getAnswerTopicIndex = (id: string) => {
      let answerTopicIndex = answerTopicIndexs.value.find(item => item.id === id)
      if (answerTopicIndex) {
        return answerTopicIndex
      } else {
        answerTopicIndex = { id, topicIndex: 0 }
        answerTopicIndexs.value.push(answerTopicIndex)
        return answerTopicIndex
      }
    }

    /**
     * 设置当前练习的考点
     * @param baseTypeCode
     * @param knowId
     */
    const setKnowExerciseIndex = (baseTypeCode: string, knowTypeCode: string, knowId: number) => {
      let knowExerciseIndex = knowExerciseIndexs.value.find(item => item.baseTypeCode === baseTypeCode)
      if (knowExerciseIndex) {
        knowExerciseIndex.baseTypeCode = baseTypeCode
        knowExerciseIndex.knowTypeCode = knowTypeCode
        knowExerciseIndex.knowId = knowId
      } else {
        knowExerciseIndexs.value.push({ baseTypeCode, knowTypeCode, knowId })
      }
    }

    /**
   * 获取当前练习的考点
   * @param baseTypeCode
   * @param knowId
   */
    const getKnowExerciseIndex = (baseTypeCode: string) => {
      let knowExerciseIndex = knowExerciseIndexs.value.find(item => item.baseTypeCode == baseTypeCode)
      if (knowExerciseIndex) {
        return knowExerciseIndex
      } else {
        return undefined
      }
    }

    return {
      questionAnswers, questionAnswerErrors, questionCollects, answerTopicIndexs, knowExerciseIndexs, knowsExerciseProgress,
      addQuestionAnswer, addQuestionCollect, removeQuestionCollect, setAnswerTopicIndex, getAnswerTopicIndex,
      setKnowExerciseIndex, getKnowExerciseIndex
    }
  },
  {
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
